﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using ADONETDataServicesExtension.Utils;

namespace ADONETDataServicesExtension.Client
{
    public class ParameterReplacerVisitor : FrameworkEncapsulatedType<object>
    {
        internal const string FrameworkTypeName = "System.Data.Services.Client.ParameterReplacerVisitor";

        public ParameterReplacerVisitor(object parameterReplacerVisitor)
            : base(parameterReplacerVisitor)
        {
        }
        private ParameterReplacerVisitor(ParameterExpression oldParameter, Expression newExpression)
        {
            FrameworkObject = ReflectionHelper.CreateInstance(FrameworkTypeName, oldParameter, newExpression);
        }

        public static Expression Replace(Expression expression, ParameterExpression oldParameter, Expression newExpression)
        {
            return new ParameterReplacerVisitor(oldParameter, newExpression).Visit(expression);
        }

        public Expression Visit(Expression exp)
        {
            return ReflectionHelper.InvokeMethod<Expression>(FrameworkTypeName, "Visit", FrameworkObject, exp);
        }
    }
}
